package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemDBHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TextView;

public class AdemDetailActivity extends Activity {

	private AdemDBHelper db = null;
	String id;
	String ty;
	String title;
	String detail;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_detail);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		Log.i("coba", ""+Build.VERSION.SDK_INT);
		
		Bundle ex = getIntent().getExtras();
		id = ex.getString("pid");
		ty = ex.getString("type");

		db = new AdemDBHelper(this);

		Cursor c;
		if (ty.equals("info"))
			c = db.getDetailInfo(id);
		else
			c = db.getDetailNews(id);

		if (c.moveToFirst()) {
			do {
				title = c.getString(c.getColumnIndex("title")).toString();
				detail = c.getString(c.getColumnIndex("detail")).toString();
			} while (c.moveToNext());
		}

		TextView t = (TextView) findViewById(R.id.id_title);
		t.setText(title);

		TextView d = (TextView) findViewById(R.id.id_detail);
		d.setText(Html.fromHtml(detail));

		db.close();
		
		/*
		try {
			FileInputStream fis =this.openFileInput(NAME_OF_FILE);
			HttpFileUploader htfu = new HttpFileUploader("http://arif.staff.amikom.ac.id/pemr.web/index.php/main/service","noparamshere", NAME_OF_FILE);
			htfu.doStart(fis);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();			
		}
		*/
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_detail_menu, menu);
		return true;				
			
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		String url;
		if (ty.equals("info"))
			url = "http://www.amikom.ac.id/index.php/main/info_kampus/id/" + id;
		else
			url = "http://www.amikom.ac.id/index.php/main/berita/" + id;

		switch (item.getItemId()) {
		case R.id.id_open_url:

			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(url));

			startActivity(i);
			return true;
		case R.id.id_share:

			String text = title + "\n" + url + "\n"
					+ "shared via: AmikomSocial for Android";

			final Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, title);
			intent.putExtra(Intent.EXTRA_TEXT, text);

			startActivity(intent);

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	@Override
	public void startManagingCursor(Cursor c) {

		if (Build.VERSION.SDK_INT < 11) {
			super.startManagingCursor(c);
		}
	}

}
